package com.wxxymaker.controller;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.http.Request;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.hook.annotation.AdminIgnore;
import com.wxxymaker.hook.annotation.AdminRequire;
import com.wxxymaker.model.entity.Article;
import com.wxxymaker.service.ArticleService;
import io.github.biezhi.anima.page.Page;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Soft
 * @Date: 2018/6/6 16:03
 * @Desc: 文章控制器
 */
@Path(value = "/article", restful = true)
@AdminRequire
public class ArticleController {

    @Inject
    private ArticleService service;

    @AdminIgnore
    @PostRoute("/addLike/:id")
    public RestResponse addLike(@PathParam int id) {
        try {
            service.addLike(id);
            return RestResponse.ok();
        } catch (Exception e) {
            return RestResponse.fail();
        }
    }

    @AdminIgnore
    @PostRoute("/addView/:id")
    public RestResponse addView(@PathParam int id) {
        try {
            service.addView(id);
            return RestResponse.ok();
        } catch (Exception e) {
            return RestResponse.fail();
        }
    }

    @PostRoute("/add")
    public RestResponse add(Article article) {
        Article add = service.add(article);
        if (add.getId() != null && add.getId() > 0) {
            return RestResponse.ok(add);
        }
        return RestResponse.fail("保存文章失败！");
    }

    @PostRoute("/delete/:id")
    public RestResponse delete(@PathParam int id) {
        if (id > 0) {
            Article article = service.delete(id);
            if (article != null) {
                return RestResponse.ok(article);
            }
        }
        return RestResponse.fail("删除失败！");
    }

    @PostRoute("/change")
    public RestResponse change(Article article) {
        if (service.change(article)) {
            return RestResponse.ok(article);
        }
        return RestResponse.fail("修改失败！");
    }

    @AdminIgnore
    @PostRoute("/one/:id")
    public RestResponse findOne(@PathParam int id) {
        if (id > 0) {
            Article one = service.findOne(id);
            if (null != one) {
                return RestResponse.ok(one);
            }
        }
        return RestResponse.fail("查找失败！");
    }

    @AdminIgnore
    @PostRoute("/page/:pageNo")
    public RestResponse page(@PathParam int pageNo, @Param String type,
                             @Param Integer status,
                             Request request) {
        String referer = request.header("Referer");

        Map<String, Object> map = new HashMap<>();
        if (type != null) map.put("type", type);
        if (!referer.contains("/admin")) { // 用户只能看到发布过的文章
            map.put("status", 1);
        } else {
            if (status != null) {
                map.put("status", status);
            }
        }

        Page<Article> page = service.page(pageNo, map);
        if (page != null && page.getTotalRows() > 0) {
            return RestResponse.ok(page);
        }
        return RestResponse.fail();
    }

    @AdminIgnore
    @PostRoute("/like")
    public RestResponse like(@Param String title) {
        Page<Article> like = service.like(title);
        if (like != null && like.getTotalRows() > 0) {
            return RestResponse.ok(like);
        }
        return RestResponse.fail();
    }

    @PostRoute("/count")
    public RestResponse count(@Param(defaultValue = "") String type,
                              @Param(defaultValue = "") Integer status) {
        try {
            long count = service.count(type, status);
            return RestResponse.ok(count);
        } catch (Exception e) {
            return RestResponse.fail(e.getMessage());
        }
    }

    @PostRoute("/allCount")
    public RestResponse allCount() {
        Map<String, Long> countInfo = new HashMap<>();
        long newsPut     = service.count(Article.Type.NEWS, 1);
        long newsDraft   = service.count(Article.Type.NEWS, 0);
        long noticePut   = service.count(Article.Type.NOTICE, 1);
        long noticeDraft = service.count(Article.Type.NOTICE, 0);
        long resultPut   = service.count(Article.Type.RESULT, 1);
        long resultDraft = service.count(Article.Type.RESULT, 0);

        countInfo.put("all", newsPut + newsDraft + noticePut + noticeDraft + resultPut + resultDraft);
        countInfo.put("allPut", newsPut + noticePut + resultPut);
        countInfo.put("allDraft", newsDraft + noticeDraft + resultDraft);

        countInfo.put("news", newsPut + newsDraft);
        countInfo.put("newsPut", newsPut);
        countInfo.put("newsDraft", newsDraft);

        countInfo.put("notices", noticePut + noticeDraft);
        countInfo.put("noticePut", noticePut);
        countInfo.put("noticeDraft", noticeDraft);

        countInfo.put("results", resultPut + resultDraft);
        countInfo.put("resultPut", resultPut);
        countInfo.put("resultDraft", resultDraft);

        return RestResponse.ok(countInfo);
    }
}
